package cn.org.rookie.jeesdp.web.response;

import org.springframework.http.HttpStatus;

import java.util.HashMap;

public class Response extends HashMap<String, Object> {

    private Response() {
    }

    private Response(int status) {
        put("status", status);
    }

    private Response(int status, ResponseType type) {
        put("status", status);
        put("type", type.getName());
    }

    public static Response success() {
        return new Response(HttpStatus.OK.value(), ResponseType.SUCCESS).message("操作成功");
    }

    public static Response success(String message) {
        return success().message(message);
    }

    public static Response success(String message, Object data) {
        return success(message).data(data);
    }

    public static Response fail() {
        return new Response(HttpStatus.NOT_ACCEPTABLE.value(), ResponseType.WRAN).message("操作失败");
    }

    public static Response fail(String message) {
        return fail().message(message);
    }

    public static Response fail(String message, Object data) {
        return fail(message).data(data);
    }

    public static Response error() {
        return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), ResponseType.ERROR).message("服务器异常");
    }

    public static Response error(String message) {
        return error().message(message);
    }

    public static Response error(String message, Object data) {
        return error(message).data(data);
    }

    public Response code(int code) {
        put("code", code);
        return this;
    }

    public Response data(Object data) {
        put("data", data);
        return this;
    }

    public Response put(String key, Object data) {
        super.put(key, data);
        return this;
    }

    public Response message(String message) {
        put("message", message);
        return this;
    }

}
